package me.aip.mov.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.aip.mov.annotation.GlobalResult;
import me.aip.mov.annotation.PassToken;
import me.aip.mov.entity.ChatFriends;
import me.aip.mov.entity.ChatMsg;
import me.aip.mov.entity.Message;
import me.aip.mov.entity.Vod;
import me.aip.mov.response.ResponseResult;
import me.aip.mov.service.IChatFriendsService;
import me.aip.mov.service.IChatMsgService;
import me.aip.mov.service.ILoginService;
import me.aip.mov.util.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/chat")
@GlobalResult
public class ChatController {

    @Autowired
    IChatFriendsService chatFriendsService;
    @Autowired
    IChatMsgService chatMsgService;
    @Autowired
    ILoginService loginService;

    @GetMapping("/findChatUserMagList")
    public ResponseResult findChatUserMagList(HttpServletRequest request, Page page) {
        String userId = RequestUtil.getUserId(request);
        return ResponseResult.successNoEncrypt(chatMsgService.findChatUserMagList(page,userId));
    }

    /***
     * 查询用户的好友
     * */
    @PostMapping("/friends")
    public List<ChatFriends> findAllFriends(){
        String userId = "1";
        return chatFriendsService.findAllFriends(userId);
    }
    /***
     * 查询两个用户之间的聊天记录
     * */
    @GetMapping("/record")
    public ResponseResult findChatMsgRecord(HttpServletRequest request, Page page, ChatMsg msg){
        String userId = RequestUtil.getUserId(request);
        msg.setFromUserid(userId);
        return ResponseResult.successNoEncrypt(chatMsgService.findChatMsgRecord(page,msg));
    }
    /***
     * 查询两个用户之间的聊天记录
     * */
    @GetMapping("/updateUnread")
    public void updateUnread(HttpServletRequest request,String toUserId){
        String userId = RequestUtil.getUserId(request);
        chatMsgService.updateUnread(userId,toUserId);
    }

    /***
     * 查询两个用户之间的聊天记录
     * */
    @GetMapping("/getUnReadCount")
    public Integer getUnReadCount(HttpServletRequest request){
        String userId = RequestUtil.getUserId(request);
        return chatMsgService.getUnReadCount(userId);
    }
}
